home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: file pointer(buffering problem)
- Date: 15 Mar 1996 21:23:22 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4idj8aINNr76@keats.ugrad.cs.ubc.ca>
- References: <4icaq9$1e@usc.edu>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4icaq9$1e@usc.edu>, ???
- <cfong@sunset.usc.edu> wrote:
- > I hope you all can help me on this...
- >
- > I was wondering if there is a way that I can read and write
- > a same file at the same time... without buffering the old stuff
- >
- > here is what I wanted to do:
- >
- > a text file of thousand lines:
- >
- > Jason Fong
- > ...
- > ..
- > .
- >
- > All I wanted to change is the first line to:
- >
- > Jason
- > ...
- > ..
- > .
- >
- > Is there a way that I can update the file without buffering the whole
- > file first!?!
-
- Of course, but you have to copy to a new file.
-
- This is what a stream editor does (the 'sed' program).
-
- In sed, the above could be accomplished using (among other ways):
-
- sed -e '1s/ Fong//' < infile > outfile.
-
- Sed reads its standard input, and performs the given command, which in this case
- addresses a single line (1, the first line), and tells it to do a regular
- expression substitution: change " Fong" to "". As it reads, it writes to
- standard output; in performing the above substitution, it will never buffer
- more than a couple of lines.
-
- To get it to delete the first three lines, you might do:
-
- sed -e '1,3d' < infile > outfile
-
- You can do the same thing in your C program. Read lines from one stream and
- copy to another. When you see the line you want, do the transformation.
-
- To hide the fact that two files are used, use tmpnam() to create a temporary
- file name where you can copy the lines, and then remove() the original (or
- rename() it to a backup) and rename() the temporary one to the original name.
- --
-
-